package cn.com.callback

import cn.com.elite.EliteBean
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 工单查询API
 */

public class apiWoEnd implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiWoEnd.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        try {
            //解析Json
            JSONObject inJson = JSONObject.parseObject(callJson);

            //获取页面信息参数
            String in_operation_type = inJson.getJSONObject("data").getString("operation_type"); //工单操作类型，保存工单:CLOSE 下送工单：SENDNS 退单：BACK 失败结案：FAILED 工单结案：END 接管未关闭的工单：TAKE 催单：URGE 接管关闭的工单：TAKECL REOPEN：重新打开工单 ROBACK：撤回

            String in_customername = inJson.getJSONObject("data").getString("in_customername"); //基本信息_客户名称
            String in_mobile = inJson.getJSONObject("data").getString("in_mobile"); //基本信息_联系电话
            String in_stalls_code = inJson.getJSONObject("data").getString("in_stalls_code"); //基本信息_档位编号
            String in_accept_acceptor = inJson.getJSONObject("data").getString("in_accept_acceptor"); //受理信息_受理人
            String in_accept_acceptorname = inJson.getJSONObject("data").getString("in_accept_acceptorname"); //受理信息_受理人姓名
            String in_accept_writedate = inJson.getJSONObject("data").getString("in_accept_writedate"); //受理信息_填表日期
            String in_undertake_undertaker = inJson.getJSONObject("data").getString("in_undertake_undertaker"); //承办信息_合同承办人员
            String in_undertaker_handledby = inJson.getJSONObject("data").getString("in_undertaker_handledby"); //承办信息_经办人
            String in_undertaker_mobile = inJson.getJSONObject("data").getString("in_undertaker_mobile"); //承办信息_联系电话
            String in_tenant_address = inJson.getJSONObject("data").getString("in_tenant_address"); //申办信息_住址
            String in_tenant_code = inJson.getJSONObject("data").getString("in_tenant_code"); //申办信息_身份证号
            String in_tenant_hometown = inJson.getJSONObject("data").getString("in_tenant_hometown"); //申办信息_籍贯
            String in_tenant_mobile = inJson.getJSONObject("data").getString("in_tenant_mobile"); //申办信息_联系电话
            String in_tenant_name = inJson.getJSONObject("data").getString("in_tenant_name"); //申办信息_承租人姓名
            String in_vehicle_carno = inJson.getJSONObject("data").getString("in_vehicle_carno"); //车辆信息_车牌号码
            String in_vehicle_driver_license_name = inJson.getJSONObject("data").getString("in_vehicle_driver_license_name"); //车辆信息_驾驶证姓名
            String in_vehicle_driver_license_number = inJson.getJSONObject("data").getString("in_vehicle_driver_license_number"); //车辆信息_驾驶证号码
            String in_vehicle_driving_license_name = inJson.getJSONObject("data").getString("in_vehicle_driving_license_name"); //车辆信息_行驶证姓名
            String in_vehicle_driving_license_number = inJson.getJSONObject("data").getString("in_vehicle_driving_license_number"); //车辆信息_行驶证号码
            String in_vehicle_model = inJson.getJSONObject("data").getString("in_vehicle_model"); //车辆信息_车型
            String in_sublet_old_contract = inJson.getJSONObject("data").getString("in_sublet_old_contract"); //转租信息_原合同名称
            String in_sublet_old_tenant = inJson.getJSONObject("data").getString("in_sublet_old_tenant"); //转租信息_原承租人
            String in_unsublet_retreat_deposit_reason = inJson.getJSONObject("data").getString("in_unsublet_retreat_deposit_reason"); //退租信息_退押原因
            String in_fitment_builder = inJson.getJSONObject("data").getString("in_fitment_builder"); //装修信息_施工人
            String in_fitment_construction_time = inJson.getJSONObject("data").getString("in_fitment_construction_time"); //装修信息_施工时间
            String in_fitment_construction_unit = inJson.getJSONObject("data").getString("in_fitment_construction_unit"); //装修信息_施工单位
            String in_fitment_fire = inJson.getJSONObject("data").getString("in_fitment_fire"); //装修信息_是否动火
            String in_fitment_identity_number = inJson.getJSONObject("data").getString("in_fitment_identity_number"); //装修信息_身份证号
            String in_fitment_valid_documents = inJson.getJSONObject("data").getString("in_fitment_valid_documents"); //装修信息_有效证件
            String in_contract_area = inJson.getJSONObject("data").getString("in_contract_area"); //合同信息_面积
            String in_contract_business_type = inJson.getJSONObject("data").getString("in_contract_business_type"); //合同信息_从事何种经营
            String in_contract_lease_term = inJson.getJSONObject("data").getString("in_contract_lease_term"); //合同信息_租赁期限开始
            String in_contract_lease_term_end = inJson.getJSONObject("data").getString("in_contract_lease_term_end"); //合同信息_租赁期限结束
            String in_contract_operating_area = inJson.getJSONObject("data").getString("in_contract_operating_area"); //合同信息_经营区域
            String in_contract_stalls_code = inJson.getJSONObject("data").getString("in_contract_stalls_code"); //合同信息_档位号
            String in_contract_title = inJson.getJSONObject("data").getString("in_contract_title"); //合同信息_合同名称
            String in_coast_decoration_deposit = inJson.getJSONObject("data").getString("in_coast_decoration_deposit"); //费用信息_装修押金
            String in_coast_deposit_amount = inJson.getJSONObject("data").getString("in_coast_deposit_amount"); //费用信息_押金金额
            String in_coast_deposit_summary = inJson.getJSONObject("data").getString("in_coast_deposit_summary"); //费用信息_押金汇总
            String in_coast_earnest_money = inJson.getJSONObject("data").getString("in_coast_earnest_money"); //费用信息_保证金
            String in_coast_electricity_bill = inJson.getJSONObject("data").getString("in_coast_electricity_bill"); //费用信息_电费
            String in_coast_electricity_deposit = inJson.getJSONObject("data").getString("in_coast_electricity_deposit"); //费用信息_电押金
            String in_coast_electricity_service_charge = inJson.getJSONObject("data").getString("in_coast_electricity_service_charge"); //费用信息_需缴供电服务费
            String in_coast_lease_deposit = inJson.getJSONObject("data").getString("in_coast_lease_deposit"); //费用信息_租赁押金
            String in_coast_monthly_management_fee = inJson.getJSONObject("data").getString("in_coast_monthly_management_fee"); //费用信息_每月管理费
            String in_coast_monthly_rent = inJson.getJSONObject("data").getString("in_coast_monthly_rent"); //费用信息_每月租金
            String in_coast_renamed_money = inJson.getJSONObject("data").getString("in_coast_renamed_money"); //费用信息_更名费
            String in_coast_summary_payment = inJson.getJSONObject("data").getString("in_coast_summary_payment"); //费用信息_缴费汇总
            String in_coast_toll_standard = inJson.getJSONObject("data").getString("in_coast_toll_standard"); //费用信息_收费标准
            String in_coast_water_deposit = inJson.getJSONObject("data").getString("in_coast_water_deposit"); //费用信息_水押金
            String in_coast_water_fee = inJson.getJSONObject("data").getString("in_coast_water_fee"); //费用信息_水费
            String in_batchbid_contract_content = inJson.getJSONObject("data").getString("in_batchbid_contract_content"); //批量申办信息_合同主要内容
            String in_batchbid_handling_department = inJson.getJSONObject("data").getString("in_batchbid_handling_department"); //批量申办信息_经办部门
            String in_batchbid_operating_area = inJson.getJSONObject("data").getString("in_batchbid_operating_area"); //批量申办信息_经营区域
            String in_batchbid_stalls_subtotal = inJson.getJSONObject("data").getString("in_batchbid_stalls_subtotal"); //批量申办信息_档位数小计
            String in_categorylable_inside_outside = inJson.getJSONObject("data").getString("in_categorylable_inside_outside"); //类别标签_场内客户_场外客户
            String in_categorylable_retreat_deposit_ty = inJson.getJSONObject("data").getString("in_categorylable_retreat_deposit_ty"); //类别标签_退押_更名退押
            String in_categorylable_stalls_orarea_deco = inJson.getJSONObject("data").getString("in_categorylable_stalls_orarea_deco"); //类别标签_档位装修_公区装修
            String in_print_flag = inJson.getJSONObject("data").getString("in_print_flag"); //打印标记
            String in_workorder_type = inJson.getJSONObject("data").getString("in_workorder_type"); //工单类型
            String in_elitetask_modifieddate = inJson.getJSONObject("data").getString("in_elitetask_modifieddate"); // 期望解决时间
            String in_elitetask_priority = inJson.getJSONObject("data").getString("in_elitetask_priority"); // 紧急程度
            String in_wo_type = inJson.getJSONObject("data").getString("in_wo_type"); // 处理类型
            String in_workorderflowdetail = inJson.getJSONObject("data").getString("in_workorderflowdetail"); // 工单处理内容描述
            String in_repair_fee = inJson.getJSONObject("data").getString("in_repair_fee"); // 维修费用
            String in_repair_aera_type = inJson.getJSONObject("data").getString("in_repair_aera_type"); // 维修区域类型
            String in_repair_flag = inJson.getJSONObject("data").getString("in_repair_flag"); // 是否可以维修

            String in_maintask_flag = inJson.getJSONObject("data").getString("in_maintask_flag"); //是否为多任务标记

            String in_wostepcomments = inJson.getJSONObject("data").getString("in_wostepcomments"); // 步骤处理意见
            String in_modifieddate = inJson.getJSONObject("data").getString("in_modifieddate"); // 操作时间
            String in_expireddate = inJson.getJSONObject("data").getString("in_expireddate"); // 工单过期时间
            String in_stepexpired = inJson.getJSONObject("data").getString("in_stepexpired"); // 步骤过期时间
            String in_stepstart = inJson.getJSONObject("data").getString("in_stepstart"); // 步骤开始时间
            String in_cur_expiredlen = inJson.getJSONObject("data").getString("in_cur_expiredlen"); // 当前工单过期时长单位为s
            String in_cur_expiredflag = inJson.getJSONObject("data").getString("in_cur_expiredflag"); // 当前工单是否过期标志 1为已过期 0为未过期
            String in_workstarttime = inJson.getJSONObject("data").getString("in_workstarttime"); // 当前步骤操作开始时间
            String in_workendtime = inJson.getJSONObject("data").getString("in_workendtime"); // 当前步骤操作结束时间

            String in_s_objetive_guid = inJson.getJSONObject("data").getString("in_s_objetive_guid"); //当前objective_guid
            String in_s_elitetask_guid = inJson.getJSONObject("data").getString("in_s_elitetask_guid"); //当前elitetask_guid
            String in_s_customer_guid = inJson.getJSONObject("data").getString("in_s_customer_guid"); //当前customer_guid
            String in_objectivetype_id = inJson.getJSONObject("data").getString("in_objectivetype_id"); //当前customer_guid
            String in_objectivesubstatus= inJson.getJSONObject("data").getString("in_objectivesubstatus"); //当前工单操作状态
            String in_objectivestatus= inJson.getJSONObject("data").getString("in_objectivestatus"); //当前工单状态
            String in_curstepstatus= inJson.getJSONObject("data").getString("in_curstepstatus"); //当前任务状态
            String in_workorder_id= inJson.getJSONObject("data").getString("in_workorder_id"); //工单id
            String in_oldworkerorderguid= inJson.getJSONObject("data").getString("in_oldworkerorderguid"); //关联工单guid
            String in_wo_contact_flag= inJson.getJSONObject("data").getString("in_wo_contact_flag"); //工单关联标记

            String in_s_newcust_flag = inJson.getJSONObject("data").getString("in_s_newcust_flag"); //是否为新客户标记
            String in_r_map_id = inJson.getJSONObject("data").getString("in_r_map_id"); //当前mapid
            String in_r_node_id = inJson.getJSONObject("data").getString("in_r_node_id"); //当前nodeid
            String in_s_userid = inJson.getJSONObject("data").getString("in_s_userid"); //当前staffid
            String in_r_rolegroup_id = inJson.getJSONObject("data").getString("in_r_rolegroup_id"); //当前Rolegroupid
            String in_oldhandlegroup_id = inJson.getJSONObject("data").getString("in_oldhandlegroup_id");// 原处理组
            String in_oldstaff_id = inJson.getJSONObject("data").getString("in_oldstaff_id");// 原处理人
            String in_r_send_group = inJson.getJSONObject("data").getString("in_r_send_group"); //下送至组
            String in_r_send_staff = inJson.getJSONObject("data").getString("in_r_send_staff"); //下送至人
            String in_r_send_node = inJson.getJSONObject("data").getString("in_r_send_node"); //下送至nodeid
            String in_r_back_group = inJson.getJSONObject("data").getString("in_r_back_group"); //退单至组
            String in_r_back_staff = inJson.getJSONObject("data").getString("in_r_back_staff"); //退单至人
            String in_r_back_node = inJson.getJSONObject("data").getString("in_r_back_node"); //退单至节点

            String[] data = new String[3];
            String re_objectivestatus = "";
            Integer task_num = 0;

            String queryTaskNumSQL = "select count(*) as task_num from elitetask where objective_guid = '" + in_s_objetive_guid + "'";
            log.debug("执行结案时task数量查询SQL:" + queryTaskNumSQL);
            List queryTaskNumList = eliteBean.chaxun_sql(queryTaskNumSQL, "dataSource");
            task_num = queryTaskNumList.get(0).get("task_num");
            log.debug("task_num:" + task_num);

            if (task_num>1)
            {
                re_objectivestatus = "OPEN";

            }else
            {
                re_objectivestatus = "CLOSE";
            }

            //插入步骤信息表
            data[0]  = "insert into workorderstep3(workorderstep_guid,workorder_guid,oldhandleby_id,oldhandlegroup_id,handleby_id,handlegroup_id,workorderstepstatus,comments," +
                    "modifieddate,from_stepidmap,to_stepidmap,stepexpired,stepstart,cur_expiredlen,cur_expiredflag,workstarttime,workendtime) " +
                    "values (uuid(), '" + in_s_objetive_guid + "','" + in_oldstaff_id + "','" + in_oldhandlegroup_id + "','" + in_r_send_staff + "','" + in_r_send_group + "','" + in_operation_type + "','" + in_wostepcomments + "','" +
                    in_modifieddate + "','" + in_r_node_id + "','" + in_r_send_node + "','" + in_stepexpired + "','" + in_stepstart + "','" + in_cur_expiredlen + "','" + in_cur_expiredflag + "','" + in_workstarttime + "','" + in_workendtime + "')";
            //更新objective
            data[1] = "update objective set " +
                    "handleby_id = '" + in_r_send_staff +"',"+
                    "handlegroup_id = '" + in_r_send_group +"',"+
                    "expireddate = '" + in_expireddate +"',"+
                    "objectivestatus = '" + re_objectivestatus +"',"+
                    "comments = '" + in_wostepcomments +"',"+
                    "modifiedby = '" + in_s_userid +"',"+
                    "modifieddate = '" + in_modifieddate +"',"+
                    "flowdetail = '" + in_workorderflowdetail +"',"+
                    "objectivesubstatus = '" + in_objectivesubstatus +"' "+
                    "where objective_guid = '" + in_s_objetive_guid + "'";
            //删除elitetask
            data[2] = "delete from elitetask where objective_guid = '" + in_s_objetive_guid + "'" + " and elitetask_guid = '" + in_s_elitetask_guid + "'";
            log.debug("当前执行更新结果SQL:" + data.toString());
            def dataresult = eliteBean.updateBatch(data, 'dataSource');
            log.debug("当前执行更新结果SQL结果:" + dataresult.toString());

            long endTime = System.currentTimeMillis();
            result.put("code", 1);
            result.put("msg", "处理完成、调用问题处理时长(毫秒):" + (endTime - startTime));

        } catch (Exception e) {
            result.put("code", 1008);
            result.put("msg", e.getMessage());
        }

        return result.toString();

    }
}